/* 
   Copyright 2012 Ryne Okimoto

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package tests;

import static org.junit.Assert.*;

import org.junit.Test;

import recipe.Ingredient;

public class IngredientTest {

	@Test
	public void testParseAmount() {
		String blank = "";
		String numbers = "1234";
		String letters = "abcd";
		String charDec = "a.";
		String decChar = ".l";
		String decFrac = "45/892.34";
		
		String fraction = "3/4";
		String doublefrac = "3//9";
		String noBefore = "/10";
		String noAfter = "1/";
		
		String decimal = "1.234";
		String doubledec = "143..542";
		String noFirstDigit = ".930";
		String noLastDigit = "1.";
		String nonConDec = "64.803.7";
		
		assertTrue(
				Ingredient.parseAmount(numbers) > 0 
				&& Ingredient.parseAmount(letters) == 0 
				&& Ingredient.parseAmount(decimal) > 0
				&& Ingredient.parseAmount(fraction) > 0
				&& Ingredient.parseAmount(doubledec) == 0
				&& Ingredient.parseAmount(blank) == 0
				&& Ingredient.parseAmount(noFirstDigit) == 0
				&& Ingredient.parseAmount(noLastDigit) == 0
				&& Ingredient.parseAmount(doublefrac) == 0
				&& Ingredient.parseAmount(noBefore) == 0
				&& Ingredient.parseAmount(noAfter) == 0
				&& Ingredient.parseAmount(charDec) == 0
				&& Ingredient.parseAmount(decChar) == 0
				&& Ingredient.parseAmount(nonConDec) == 0
				&& Ingredient.parseAmount(decFrac) == 0
		);
		
		assertEquals("should be equal", 1.2, Ingredient.parseAmount("1.2"), 0);
		assertEquals("should be equal", 0.75, Ingredient.parseAmount("3/4"), 0);
	}
	
	@Test
	public void testScaleAmount(){
		Ingredient i = new Ingredient();
		i.setAmount("0.75");
		
		i.scaleAmount(2);
		
		assertEquals("Should be equal", "1.50", i.getAmount());
	}
	
	@Test
	public void testToString(){
		Ingredient i = new Ingredient();
		String s = "1 c sugar";
		
		i.setIngredientName("sugar");
		i.setAmount("1");
		i.setUnit("c");
		
		assertEquals("Should be equal", s, i.toString());
	}
	
	@Test
	public void testValidateUnit(){
		for(int i = 0; i < Ingredient.getUnits().length; i++){
			assertTrue(Ingredient.validateUnit(Ingredient.getUnits()[i]));
		}
		
		assertFalse(Ingredient.validateUnit("a"));
	}
	
	@Test
	public void testFormatDouble(){
		assertEquals("Should be equal", "1.94", Ingredient.formatDouble(1.94385));
	}
	
	@Test
	public void testConvertUnit(){
		// TODO add more measurement units to test
		
		Ingredient i = new Ingredient();
		i.setAmount("1");
		i.setUnit(Ingredient.UNIT_METRIC_ML);
		
		assertEquals("Should be equal", 0.033814, i.convertUnit(), 0);
	}

}
